/**
 * Created by admin on 2018/11/16.
 */
const mysqlUtil = require('../utils/mysql_util');
const dao = require('../dao/scoreInfoDao');
const logUtil = require('../utils/log_util');
const _ = require('lodash');


/**
 * 保存信用评分信息
 * @param ctx
 * @returns {Promise.<void>}
 */
exports.save = async (ctx) => {
  const scoreInfo = ctx.request.body;
  if (!scoreInfo.mobile) { throw new Error('请输入手机号码！'); }
  const oldScoreInfo = await mysqlUtil.execute(dao.select(scoreInfo.mobile));
  if(oldScoreInfo.length===0) {
    ctx.body = await mysqlUtil.execute(dao.insert(scoreInfo))
  }
  else {
    ctx.body = await mysqlUtil.execute(dao.update(scoreInfo))
  }
};

/**
 * 查询信用评分信息
 * @param ctx
 * @returns {Promise.<void>}
 */
exports.select = async (ctx) => {
  const query = ctx.query;
  if (!query.m) { throw new Error('请输入手机号码！'); }
  const mobile=query.m;
  await mysqlUtil.execute(dao.updateQueryTimes(mobile));
  ctx.body = await mysqlUtil.execute(dao.select(mobile));
};
